using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;


namespace IcyTowerMobile
{
	/// <summary>
	/// Singleton class for accessing the isolated storage
	/// (for e.g. highscores or app settings).
	/// </summary>
	public class IsolatedStorageManager
	{
		public const string FILENAME_HIGHSCORES = "highscores.lst";
		public const string FILENAME_SETTINGS = "icytowersettings.lst";

		public const string KEY_SOUNDS = "sounds";

		private IsolatedStorageFile storage;
		private IsolatedStorageSettings settings;

		private static IsolatedStorageManager instance;

		/// <summary>
		/// Hide constructor for outter classes.
		/// </summary>
		private IsolatedStorageManager()
		{
			storage = IsolatedStorageFile.GetUserStoreForApplication();
			if (!storage.FileExists(IsolatedStorageManager.FILENAME_SETTINGS))
			{
				storage.CreateFile(FILENAME_SETTINGS);
			}
			settings = IsolatedStorageSettings.ApplicationSettings;
		}

		/// <summary>
		/// Retrieves the static instance of the isolated storage manager
		/// or creates a new one if it does not exist yet.
		/// </summary>
		/// <returns></returns>
		public static IsolatedStorageManager GetInstance()
		{
			if (instance == null)
			{
				instance = new IsolatedStorageManager();
			}
			return instance;
		}

		/// <summary>
		/// Checks if game sound is enabled.
		/// </summary>
		/// <returns>True if game sound is enabled. False otherwise.</returns>
		public bool IsSoundEnabled()
		{
			bool sounds;
			bool keyFound = settings.TryGetValue<bool>(KEY_SOUNDS, out sounds);
			sounds = keyFound ? sounds : true;
			return sounds;
		}

		/// <summary>
		/// Sets the game sound enabled flag.
		/// </summary>
		/// <param name="soundEnabled">True if game sound is enabled. False otherwise.</param>
		public void SetSoundEnabled(bool soundEnabled)
		{
			settings[KEY_SOUNDS] = soundEnabled;
		}

		/// <summary>
		/// Saves the given highscore data to isolated storage.
		/// </summary>
		/// <param name="data">Highscore data which should be saved.</param>
		public void SaveHighScores(HighscoreData data)
		{
			IsolatedStorageFileStream stream = storage.OpenFile(FILENAME_HIGHSCORES, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);

			try
			{
				// Convert the object to XML data and put it in the stream
				XmlSerializer serializer = new XmlSerializer(typeof(HighscoreData));
				serializer.Serialize(stream, data);
			}
			finally
			{
				// Close the file
				stream.Close();
			}
		}

		/// <summary>
		/// Loads the highscores from isolated storage.
		/// </summary>
		/// <returns>Retrieved highscore data.</returns>
		public HighscoreData LoadHighScores()
		{
			HighscoreData data;

			IsolatedStorageFileStream stream = storage.OpenFile(FILENAME_HIGHSCORES, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
			try
			{
				// Read the data from the file
				XmlSerializer serializer = new XmlSerializer(typeof(HighscoreData));
				data = (HighscoreData)serializer.Deserialize(stream);
			}
			catch (Exception e)
			{
				// just catch exception, highscores could not be loaded
				data = new HighscoreData();
			}
			finally
			{
				// Close the file
				stream.Close();
			}

			return data;
		}
	}
}
